package com.wqtz.chechai.view.activities;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.view.View;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;

import com.orhanobut.logger.Logger;
import com.wqtz.chechai.R;
import com.wqtz.chechai.adapter.AdSearchStatusAdapter;
import com.wqtz.chechai.adapter.SearchHisAdapter;
import com.wqtz.chechai.adapter.SearchResultAdapter;
import com.wqtz.chechai.base.BaseActivity;
import com.wqtz.chechai.bean.MsgBean;
import com.wqtz.chechai.bean.OrderBean;
import com.wqtz.chechai.presenter.BasePresent;
import com.wqtz.chechai.utils.SPUtils;
import com.wqtz.chechai.utils.StringUtils;
import com.wqtz.chechai.utils.ToastUtils;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 项目名称： CheChai
 * 创建人 : Liberation
 * 创建时间: 2017/2/22 15:47
 */

public class SearchActivity extends BaseActivity implements SearchResultAdapter.QuoteItemClickCallBack, SearchHisAdapter.QuoteHisItemClickCallBack {

	String searchhis;
	@BindView(R.id.adsearch)
	TextView mAdsearch;
	@BindView(R.id.et)
	EditText mEt;
	@BindView(R.id.search)
	TextView mSearch;
	@BindView(R.id.lv_his)
	ListView mLvHis;
	@BindView(R.id.lv_result)
	ListView mLvResult;
	@BindView(R.id.notice)
	TextView mNotice;
	private String[] sears;
	private List<String> datas;
	private SearchHisAdapter adapter;
	private SearchResultAdapter searchAdapter;
	private int mHour=3;
	private int mMinuts=05;
	List<OrderBean> data;//模拟数据
	int requsetNum = 0;

	@Override
	public void init() {
		ButterKnife.bind(this);
		datas = new ArrayList<>();
		data = new ArrayList<>();
		adapter = new SearchHisAdapter(R.layout.item_searchhis);
		mLvHis.setAdapter(adapter);
		searchAdapter = new SearchResultAdapter(R.layout.item_search_result);
		mLvResult.setAdapter(searchAdapter);
		searchAdapter.setClickListen(this);
		adapter.setClickListen(this);

	}

	@Override
	protected void onResume() {
		super.onResume();
		getHisRefresh();
	}

	@Override
	public void loadData() {

	}

	@Override
	public int setContentViews() {
		return R.layout.activity_search;
	}

	@Override
	public BasePresent createPresenter() {
		return null;
	}


	@OnClick({R.id.adsearch, R.id.search, R.id.notice})
	public void onClick(View view) {
		switch (view.getId()) {
			case R.id.adsearch:
				Intent intent = new Intent(this, AdvanceSearchActivity.class);
				startActivity(intent);
				break;
			case R.id.search:
				if (!StringUtils.isEmpty(mEt.getText().toString())) {
					String temp = SPUtils.get(this, "search", "").toString();
					SPUtils.put(this, "search", temp + mEt.getText().toString() + "#");
					createData(5,true,mEt.getText().toString());
					mLvHis.setVisibility(View.GONE);
					mLvResult.setVisibility(View.VISIBLE);
				} else {
					ToastUtils.show(this, "请输入搜索的车型", 2000);
				}
				break;
			case R.id.notice:
				if (datas.size() != 0) {
					SPUtils.remove(this, "search");
					getHisRefresh();
				}
				break;
		}
	}


	public void getHisRefresh() {
		searchhis = SPUtils.get(this, "search", "").toString();
		datas.clear();
		if ("".equals(searchhis)) {
			mNotice.setVisibility(View.GONE);
			mLvHis.setVisibility(View.GONE);
		} else {
			sears = searchhis.split("#");
			Logger.e(sears.length + "");
			for (int i = 0; i < sears.length; i++) {
				datas.add(sears[i]);
			}
			adapter.notifyDataSetChanged();
			mLvResult.setVisibility(View.GONE);
			mLvHis.setVisibility(View.VISIBLE);
			mNotice.setVisibility(View.VISIBLE);
			mNotice.setClickable(true);
			mNotice.setText("清空搜索历史");
			mNotice.setTextColor(Color.parseColor("#AD2928"));

		}
		adapter.addData(datas);
	}

	/**
	 * @param request    请求的条数
	 * @param isResfresh 是否刷新
	 * @param carName 车辆名字仅做测试
	 */
	private void createData(int request, boolean isResfresh,String carName) {
		data.clear();
		if (isResfresh) {
			requsetNum = 15;
		} else {
			requsetNum += request;
		}
		for (int i = 0; i < requsetNum; i++) {
			//		Logger.e(requsetNum + "请求的数据量");
			OrderBean d = new OrderBean();
			d.setCarVIN("1F2B5D888FA44FRT" + i % 10);
			d.setType(i % 4); //四种状态 0 1 2 3
			d.setVisible(true);//是否可见
			d.setCarModel(carName + String.valueOf(i));
			d.setCarBuyTime("6年" + String.valueOf(i % 12) + "个月");
			d.setMileage(String.valueOf(i + 20) + "km");
			d.setCarStatus("一抵");
			d.setAssumPrice(20 + i + "万");
			d.setShouldPay((20 + i - 3) + "万");
			d.setPrice1(20 + i + "");
			d.setPrice2(21 + i + "");
			d.setPrice3(22 + i + "");
			d.setLastModefyTime(mHour + ":" + mMinuts);
			List<MsgBean> msgs = new ArrayList<>();
			for (int j = 0; j < 2; j++) {
				MsgBean msgBean = new MsgBean();
				msgBean.time = "   2016-9-" + i % 30 + " 12:55:12";
				msgBean.title = "【张婷】已审核了该报价，价格调整为" + (10 + i) + "万元";
				msgs.add(msgBean);
			}
			d.setMsgs(msgs);
			data.add(d);
		}
		searchAdapter.clearData();
		searchAdapter.addData(data);

	}

    //搜索结果点击事件
	@Override
	public void oItemClickItem(int positon) {
		ToDetail(positon);
	}

	private void ToDetail(int position) {
		Intent intent = new Intent(this, QuoteDetailActivity.class);
		OrderBean o = data.get(position);
		intent.putExtra("cityId", o.getCityId());
		intent.putExtra("provinceId", o.getProvinceId());
		intent.putExtra("carModelId", o.getCarModelId());
		intent.putExtra("carModel", o.getCarModel());
		intent.putExtra("buyTime", o.getCarBuyTime());
		intent.putExtra("mileage", o.getMileage());
		intent.putExtra("carStatus", o.getCarStatus());
		intent.putExtra("price1", o.getPrice1());
		intent.putExtra("price2", o.getPrice2());
		intent.putExtra("price3", o.getPrice3());
		intent.putExtra("assumPrice", o.getAssumPrice());
		intent.putExtra("VIN", o.getCarVIN());
		intent.putExtra("type", o.getType());
		intent.putExtra("shouldpay", o.getShouldPay());
		intent.putExtra("msgs", (Serializable) o.getMsgs());
		startActivity(intent);
	}

	@Override
	public void QuoteHisItemClickCallBack(int positon) {
		mNotice.setText("搜索无结果");
		mNotice.setTextColor(Color.BLACK);
		mNotice.setVisibility(View.VISIBLE);
		mLvHis.setVisibility(View.GONE);
		mLvResult.setVisibility(View.GONE);
		mNotice.setClickable(false);
	}
}
